
/**
 * 描述 当前函数仅限在全局 有 uni 函数下使用 该库用于uni-app
 * 时间：2022-04-11
 * 作者：自夏
 */

import { isType, hexadecimal } from '../util/UtilityBox'
import { isUni, uniNavigationBarColor } from "../entity/uni_Type";
import { EncryptionToDecrypt } from '../util/EncryptionToDecrypt';


// 声明全局uni
declare const uni: any;
let isuni: isUni
try {
  isuni = 'uni' in globalThis;
} catch (e) {
  isuni = false
}


// 区分环境变量
let envConfig = {};

if (process.env.NODE_ENV === 'production') {
  envConfig = {
    baseUrl: 'http://czhapi.chemeiche.com/', //  请求域名或者IP http://server3.miaoshare.com:1026/  正式服务 http://czhapi.chemeiche.com/
    secret: 'development', // 当前开发环境
    appid: 'wx1a8be766d09a2d11', // 公众号授权的APPID  个人测试wx8d5c3b9cae510bcb 测试环境 wx1a8be766d09a2d11
    secretKey: 'STORAGEKEYCHEZHUHUI', // 存储加密项
  }
} else {
  // 本地测试  http://192.168.88.59:8088/
  envConfig = {
    baseUrl: 'http://192.168.88.59:8088/',
    secret: 'production',
    appid: 'wx8d5c3b9cae510bcb',
    secretKey: 'STORAGEKEYCHEZHUHUI',
  }
}



//存储数据
export const setStorage = (key: string, data: string) => {
  data = EncryptionToDecrypt.encryptionCryptoJS(data);
  uni.setStorage({ key, data, });
}


// 获取数据
export const getStorage = (key: string) => {
  const getData = uni.getStorageSync(key), originalText = EncryptionToDecrypt.decryptionCryptoJS(getData);
  return originalText && JSON.parse(originalText)
};

// 删除
export const removeStorage = (key: string) => uni.removeStorageSync(key)


export const clearAll = uni.clearStorageSync();


/**
 * uni.showLoading 二次封装
 *
 * https://uniapp.dcloud.io/api/ui/prompt?id=showtoast
 */
const showLoading = (title = '加载中', mask = false, param = { success() { }, fail() { }, complete() { } }) => {
  isType(mask) === 'Object' ? (mask = mask, mask = false) : (param = param, mask = false);
  return uni.showLoading({ title, mask, ...param });
}

/**
 * 隐藏 showLoading
 */
const hideLoading = () => uni.hideLoading;


/**
 * 修改顶部样式，包含字体
 * @param title 标题
 * @param NavigationBarColor 导航条背景设置
 * https://uniapp.dcloud.io/api/ui/navigationbar?id=setnavigationbartitle
 */
const haderStyle = (title: string, NavigationBarColor: { color: uniNavigationBarColor, background: any; }) => {

  isType(title) === 'String' && uni.setNavigationBarTitle({ title })

  if (isType(NavigationBarColor) === 'Object') {
    const {
      color: frontColor,
      background: backgroundColor
    } = NavigationBarColor;

    const is: Array<uniNavigationBarColor> = ['#FFFFFF', '#ffffff', '#000000'];
    if (!is.includes(frontColor)) throw new Error('color 仅支持 #ffffff 和 #000000');
    if (!hexadecimal(backgroundColor)) throw new Error('backgroundColor 背景颜色值，有效值为十六进制颜色');

    uni.setNavigationBarColor({
      frontColor,
      backgroundColor
    })
  }

}


export const Toast = (title: string, cb?: () => Function) => {
  uni.showToast({
    icon: "none",
    position: "bottom",
    title,
    complete() {
      cb instanceof Function && cb()
    }
  });
}


export const EPlatform = {
  /**App*/
  AppPlus: 'APP-PLUS',
  /**App nvue*/
  AppPlusNvue: 'APP-PLUS-NVUE',
  /**H5*/
  H5: 'H5',
  /**微信小程序*/
  MpWeixin: 'MP-WEIXIN',
  /**支付宝小程序*/
  MpAlipay: 'MP-ALIPAY',
  /**百度小程序*/
  MpBaidu: 'MP-BAIDU',
  /**字节跳动小程序*/
  MpToutiao: 'MP-TOUTIAO',
  /**QQ小程序*/
  MpQq: 'MP-QQ',
  /**360小程序*/
  Mp360: 'MP-360',
  /**微信小程序/支付宝小程序/百度小程序/字节跳动小程序/QQ小程序/360小程序*/
  Mp: 'MPAll',
  /**快应用通用(包含联盟、华为)*/
  QuickappWebview: 'quickapp-webview',
  /**快应用联盟*/
  QuickappWebviewUnion: 'quickapp-webview-union',
  /**快应用华为*/
  QuickappWebviewHuawei: 'quickapp-webview-huawei',
}

/**使用条件编译获取平台信息*/
function ifDefPlatform() {
  let platform;

  //#ifdef APP-PLUS
  platform = EPlatform.AppPlus
  //#endif

  //#ifdef APP-PLUS-NVUE
  platform = EPlatform.AppPlusNvue
  //#endif

  //#ifdef H5
  platform = EPlatform.H5
  //#endif

  //#ifdef MP-WEIXIN
  platform = EPlatform.MpWeixin
  //#endif

  //#ifdef MP-ALIPAY
  platform = EPlatform.MpAlipay
  //#endif

  //#ifdef MP-BAIDU
  platform = EPlatform.MpBaidu
  //#endif

  //#ifdef MP-TOUTIAO
  platform = EPlatform.MpToutiao
  //#endif

  //#ifdef MP-QQ
  platform = EPlatform.MpQq
  //#endif

  //#ifdef MP-360
  platform = EPlatform.Mp360
  //#endif

  //#ifdef MP
  platform = EPlatform.Mp
  //#endif

  //#ifdef quickapp-webview
  platform = EPlatform.QuickappWebview
  //#endif

  //#ifdef quickapp-webview-union
  platform = EPlatform.QuickappWebviewUnion
  //#endif

  //#ifdef quickapp-webview-huawei
  platform = EPlatform.QuickappWebviewHuawei
  //#endif

  return platform
}

/**平台类型*/
export const Platform = ifDefPlatform()
/**默认导出平台类型*/
// export default Platform
// 移动端H5
export const isH5 = Platform === EPlatform.H5
// APP 仅限uni打包出来的APP
export const isApp = Platform === EPlatform.AppPlus
// 微信小程序
export const isMpWeixin = Platform === EPlatform.MpWeixin
//判断当前是否是微信h5
// export const isWXh5 = String(navigator?.userAgent?.toLowerCase()?.match(/MicroMessenger/i)) === "micromessenger"
export const isWXh5 = /MicroMessenger/.test(window?.navigator?.userAgent)
// 支付宝H5
export const isAliPay = /AlipayClient/.test(window?.navigator?.userAgent)

export default { Platform, isH5, isApp, isMpWeixin, isWXh5, isAliPay, Toast, haderStyle, hideLoading, isuni, setStorage, getStorage, removeStorage, clearAll, showLoading }